package com.jianzhile.jobs.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.ImageColumns;
import android.provider.MediaStore.MediaColumns;
import android.text.TextUtils;

import com.jianzhile.jobs.R;
import com.jianzhile.jobs.bean.BucketEntity;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * 相册工具
 * 
 * @author liaiguo
 * 
 */
public class AlbumHelper {

	// 筛选字段
	private static final String[] mProjection = { BaseColumns._ID,
			ImageColumns.MINI_THUMB_MAGIC, MediaColumns.DATA,
			ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.BUCKET_ID };
	// 筛选条件
	private static final String mSelection = MediaStore.Images.Media.MIME_TYPE
			+ "=? or " + MediaStore.Images.Media.MIME_TYPE + "=? or "
			+ MediaStore.Images.Media.MIME_TYPE + "=?";
	// 只选择出图片格式
	private static final String[] mSelectionArgs = { "image/jpeg", "image/bmp",
			"image/png" };
	// 排序规则
	private static final String mOrderBy = MediaColumns.DATE_ADDED + " DESC";

	/**
	 * 获取照片的地址列表，根据bucketId来判断，如果bucketId为null，则获取所有图片；否则，根据bucketId筛选
	 */
	public static ArrayList<String> getPhotoPath(ContentResolver resolver,
												 String bucketId) {
		ArrayList<String> pathList = new ArrayList<String>();

		Cursor cursor = null;
		if (TextUtils.isEmpty(bucketId)) {
			// 获取所有图片
			cursor = resolver.query(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mProjection,
					mSelection, mSelectionArgs, mOrderBy);
		} else {
			// 筛选条件
			String selection = "(" + MediaStore.Images.Media.MIME_TYPE
					+ "=? or " + MediaStore.Images.Media.MIME_TYPE + "=? or "
					+ MediaStore.Images.Media.MIME_TYPE + "=?) and "
					+ ImageColumns.BUCKET_ID + "=?";
			// 只选择出图片格式
			String[] selectionArgs = { "image/jpeg", "image/bmp", "image/png",
					bucketId };
			// 获取制定bucketId的图片
			cursor = resolver.query(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mProjection,
					selection, selectionArgs, mOrderBy);
		}

		pathList = new ArrayList<String>();

		if (null == cursor) {
			return pathList;
		}

		if (cursor.getCount() > 0 && cursor.moveToFirst()) {
			do {
				// 循环遍历cursor,将数据放到list
				int dataColumnIndex = cursor
						.getColumnIndexOrThrow(MediaColumns.DATA);
				pathList.add(cursor.getString(dataColumnIndex));
			} while (cursor.moveToNext());
		}

		// 关闭cursor
		cursor.close();

		return pathList;
	}

	/**
	 * 获取相册列表
	 */
	public static ArrayList<BucketEntity> getBucketList(Context context,
														ContentResolver resolver) {
		ArrayList<BucketEntity> bucketList = new ArrayList<BucketEntity>();
		final HashSet<String> bucketIds = new HashSet<String>();

		// 按bucket id分组,由于ContentResolver不支持distinct，只能用HashSet保证bucketId唯一
		Cursor cursor = resolver.query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mProjection,
				mSelection, mSelectionArgs, mOrderBy);

		if (null == cursor) {
			return bucketList;
		}

		final int bucketIdColumn = cursor
				.getColumnIndex(ImageColumns.BUCKET_ID);
		final int bucketNameColumn = cursor
				.getColumnIndex(ImageColumns.BUCKET_DISPLAY_NAME);
		final int previewColumn = cursor.getColumnIndex(MediaColumns.DATA);

		if (cursor.getCount() > 0 && cursor.moveToFirst()) {
			// 有数据情况下
			// 先添加一个默认项，相机胶卷，其bucket id为null，以便跟其他区分开
			BucketEntity entity = new BucketEntity();
			entity.setBucketId(null);
			entity.setBucketName(context.getString(R.string.gallery));

			do {
				String bucketId = cursor.getString(bucketIdColumn);
				if (bucketIds.add(bucketId)) {
					// 用HashSet可防止bucketId被重复添加
					BucketEntity otherEntity = new BucketEntity();
					otherEntity.setBucketId(bucketId);
					otherEntity.setBucketName(cursor
							.getString(bucketNameColumn));
					otherEntity.setPreviewPath(cursor.getString(previewColumn));

					// 先给默认的 相机胶卷设置preview图
					if (null == entity.getPreviewPath()) {
						entity.setPreviewPath(cursor.getString(previewColumn));
						bucketList.add(entity);
					}

					bucketList.add(otherEntity);
				}
			} while (cursor.moveToNext());
		}

		// 关闭cursor
		cursor.close();

		return bucketList;
	}

}
